module.exports = function ($scope, $resource, $modalInstance, $modal, toaster) {

	init();
	//初始化查询全部可参与招徕奖励的机构
	function init(){
		$scope.searchform = {'loading': true};
		$resource('/api/ac/tc/ticketSaleService/getSaleForAttractList', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
				$scope.saleList = res.data;
				$scope.searchform.loading = false;
            } else {
                toaster.error({title: '', body: '查询信息失败'});
            }
		});
	}

	//全选or全不选
	$scope.selectAll = function(flag){
		angular.forEach($scope.saleList, function(item){
			item.check = flag;
		});
	}

	//保存
	$scope.ok = function() {
		var para = {
			'sale_list': []
		}
		angular.forEach($scope.saleList, function(item){
			if(item.check){
				para.sale_list.push(item);
			}
		});
		if(para.sale_list.length == 0){
			toaster.warning({title: '', body: '请选择要添加的产品'});
			return;
		}
		$resource('/api/as/atc/sale/addSale', {}, {}).save(para, function(res){
            if (res.errcode === 0) {
				toaster.success({title: '', body: '成功添加' + res.data.count + '个产品'});
				$modalInstance.close();
            } else {
                toaster.error({title: '', body: '添加失败, 请重试'});
            }
		});
    }

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
    }


};